package com.xuxueli.applyModules.commont;

import com.xuxueli.applyModules.mapper.TableIdxMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 初始化表索引
 */
@Component
@Slf4j
public class InitTableIdxSchdule {

    @Resource
    private TableIdxMapper tableIdxMapper;

    static final List<String> tableIdxList = Arrays.asList(
            "idx_user_icon:user_icon:account",
            "idx_cj:s010_cj:xh"
    );

    //添加定时任务0/5 * * * * ?
    @Scheduled(cron = "0 0 12 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void configureTasks() {
        tableIdxList.stream().forEach(item->{
            String[] arr = item.split(":");
            int countocjb= tableIdxMapper.getTableIdxByTable(arr[0], arr[1]);
            if(countocjb<=0){
                tableIdxMapper.createIdx(arr[0],arr[1],arr[2]);
            }
        });
    }
}
